package com.ccdc.lbcmsystem.controller;

import com.ccdc.lbcmsystem.mapper.ChildrenMapper;
import com.ccdc.lbcmsystem.mapper.ExamineMapper;
import com.ccdc.lbcmsystem.mapper.UserMapper;
import com.ccdc.lbcmsystem.pojo.Children;
import com.ccdc.lbcmsystem.pojo.Delchildren;
import com.ccdc.lbcmsystem.pojo.User;
import com.ccdc.lbcmsystem.util.SMSSentUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class ChildrenController {

    @Autowired
    ChildrenMapper childrenMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ExamineMapper examineMapper;

    @RequestMapping("/childrenSMSSent/{id}")
    public String SMSSent(@PathVariable("id") Integer id) throws Exception {
        Children children = childrenMapper.queryChildrenById(id);
        String guardianPhone = children.getGuardianPhone();
        System.out.println(guardianPhone);
        String host = "http://api.guoyangyun.com";
        String path = "/api/sms/smsoto.htm";
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("appkey", "1725670970202304031648128407995");
        paramMap.put("appsecret", "f421d50df5f508fd025bbd0185ef8e3c");
        paramMap.put("smsSignId", "cd4ab2b471f64a40be51a0b4f2e429e5"); // 签名ID
        paramMap.put("templateId", "32dd138ca0634a6289b7988a547aa57b"); // 模板ID
        paramMap.put("mobile",guardianPhone); // 手机号码
       try{
           HttpResponse httpResponse = SMSSentUtil.doGet(host, path, paramMap);
           System.out.println(httpResponse);
           // response body
           System.out.println(EntityUtils.toString(httpResponse.getEntity()));
       } catch (IOException e) {
           e.printStackTrace();
       }
       return "redirect:/childrenList";
    }

    @RequestMapping("/childrenList")
    public String childrenList(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                               @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(startPage, pageSize);
        List<Children> childrens = childrenMapper.queryChildrenList();
        PageInfo<Children> childrensPageInfo = new PageInfo<>(childrens);
        model.addAttribute("childrensPageInfo", childrensPageInfo);
        model.addAttribute("childrens",childrens);
        return "children/childrenList";
    }
    @RequestMapping("/searchChildren")
    public String searchChildren(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                                 @RequestParam(defaultValue = "10") Integer pageSize, String name, HttpSession session){
        PageHelper.startPage(startPage, pageSize);
        List<Children> childrens = childrenMapper.queryListChildrenByName(name);
        PageInfo<Children> childrensPageInfo = new PageInfo<>(childrens);
        session.setAttribute("name",name);
        model.addAttribute("childrensPageInfo", childrensPageInfo);
        model.addAttribute("childrens",childrens);
        return "children/childrenSearchList";
    }

    @RequestMapping("/childrenRecentlyList")
    public String childrenRecentlyList(){
        return "children/childrenRecentlyList";
    }//////////////////////////////////////////未完成

    @RequestMapping("/toChildrenAdd")
    public String tochildrenAdd(){
        return "children/childrenAdd";
    }
    @RequestMapping("/childrenAdd")
    @ResponseBody
    public String childrenAdd(Children children) {
        String msg = "";
        SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime = new Date();
        String time = sft.format(createTime);
        children.setCreateTime(time);
        children.setDeleteStatus(0);
        try {
            childrenMapper.addChildren(children);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "新增失败";
            return msg;
        }

    }

    @RequestMapping("/childrenLookInfo/{id}")
    public String childrenLookInfo(@PathVariable("id") Integer id, Model model){
        Children children = childrenMapper.queryChildrenById(id);
        model.addAttribute("children",children);
        return "children/childrenLookInfo";
    }

    @RequestMapping("/toChildrenEdit/{id}")
    public String toChildrenEdit(@PathVariable("id") Integer id, Model model){
        Children children = childrenMapper.queryChildrenById(id);
        model.addAttribute("children",children);
        return "children/childrenEdit";
    }
    @RequestMapping("/childrenEdit")
    @ResponseBody
    public String childrenEdit(String name,Integer id,Integer age,String phone,String birthday,String school,String address,
                               String guardian,String guardianPhone,String healthCondition,Integer health
            ,Integer familyStatus){
        String msg = "";
        try{
            childrenMapper.updateChildrenById(id,name, phone,birthday, age,school, address
                    ,guardian, guardianPhone,healthCondition, health,familyStatus);
            msg = "OK";
            return msg;
        }catch (Exception e){
            System.out.println(e);
            msg = "修改失败";
            return msg;
        }

    }

    @RequestMapping("/childrenDelete")
    @ResponseBody
    public String childrenDelete(Integer id, Model model, String examineUser, Delchildren delchildren, String reason){
        String msg = "";

        System.out.println(examineUser);
        try {
            User user = userMapper.queryUserByUserName(examineUser);
            Children children = childrenMapper.queryChildrenById(id);
          System.out.println(user);
            //工作人员删除 需要提交给管理员审核
            if (user.getRole().equals("1")){
                Integer deleteStatus = 2;//将删除状态码修改成 2   提交给管理员审核
                childrenMapper.delChildrenById(id,deleteStatus);
                Delchildren delchildren1 = examineMapper.queryDelChildren(id);
                if (delchildren1 != null){
                    msg = "该儿童信息已提交删除申请,请勿重复操作!";
                    return msg;
                }else {
                    delchildren.setDelUser(examineUser);
                    delchildren.setId(children.getId());
                    delchildren.setName(children.getName());
                    delchildren.setPhone(children.getPhone());
                    delchildren.setBirthday(children.getBirthday());
                    delchildren.setAge(children.getAge());
                    delchildren.setSchool(children.getSchool());
                    delchildren.setAddress(children.getAddress());
                    delchildren.setGuardian(children.getGuardian());
                    delchildren.setGuardianPhone(children.getGuardianPhone());
                    delchildren.setHealth(children.getHealth());
                    delchildren.setHealthCondition(children.getHealthCondition());
                    delchildren.setFamilyStatus(children.getFamilyStatus());
                    delchildren.setDeleteStatus(2);
                    delchildren.setReason(reason);
                    SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date createTime = new Date();
                    String time = sft.format(createTime);
                    delchildren.setCreateTime(time);
                    examineMapper.addDelChildren(delchildren);
                    msg = "OK";
                    return msg;
                }
            }else {
                Integer deleteStatus = 1;
                childrenMapper.delChildrenById(id,deleteStatus);
                msg = "OK";
                return msg;
            }

        }catch (Exception e){
            msg = "删除失败";
            return msg;
        }
    }
}
